﻿using System;
using System.Collections.Generic;

using System.Text;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// A logical control that groups a number of RadioButton controls and provides a single input point for all of them.
	/// </summary>
	public class RadioGroup : BaseFieldControl<string>
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public RadioGroup()
		{
			Tag = System.Web.UI.HtmlTextWriterTag.Span;
			Style[System.Web.UI.HtmlTextWriterStyle.Display] = "none";
		}
		#endregion
		#region PROPS
		/// <summary>
		/// Gets an immutable collection of <see cref="RadioButton"/>s.
		/// </summary>
		public List<RadioButton> Buttons
		{
			get
			{
				return this._buttons;
			}
		}
		/// <summary>
		/// Gets the <see cref="RadioButton"/> that is currently selected.
		/// </summary>
		public RadioButton SelectedButton
		{
			get
			{
				return this.Buttons.Find(b => b.Value);
			}
			set
			{
				foreach (RadioButton b in Buttons)
				{
					b.Value = (b == value);
				}
			}
		}
		/// <summary>
		/// Gets or sets the value (setting this selects the appropriate <see cref="RadioButton"/>).
		/// </summary>
		public override string Value
		{
			get
			{
				RadioButton button = this.SelectedButton;
				if (button == null)
					return null;
				else
					return button.ID;
			}
			set
			{
				foreach (RadioButton b in this.Buttons)
				{
					b.Value = string.Equals(b.ID, value);
				}
			}
		}
		#endregion
		#region METHODS
		/// <summary>
		/// Registers the specified <see cref="RadioButton"/> with this group.
		/// </summary>
		/// <param name="button"></param>
		public void Register(RadioButton button)
		{
			if (!string.Equals(button.GroupId, this.ID))
				throw new ArgumentException(string.Format("Radio button '{0}' does not belong to group '{1}' but to group '{2}'.", button.ID, this.ID, button.GroupId));
			_buttons.Add(button);
		}
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		List<RadioButton> _buttons = new List<RadioButton>();
		#endregion
		#region PROPS
		/// <summary>
		/// Gets or sets the serialized value.
		/// </summary>
		protected internal override string SerializedValue
		{
			get
			{
				return null;
			}
			set
			{
				Value = value;
			}
		}
		#endregion
		#region METHODS
		#region INIT
		/// <summary>
		/// 
		/// </summary>
		protected internal override void RegisterIncludes(IncludeCollection includes)
		{
			base.RegisterIncludes(includes);

			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.Simple.RadioGroup.js", typeof(BaseCommandControl)));
		}
		#endregion
		#region RENDER
		/// <summary>
		/// Adds type attribute valued "radiogroup".
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
		{
			base.AddAttributesToRender(writer);

			writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Type, "radiogroup");
		}
		/// <summary>
		/// Returns true if the name attribute is required, otherwise false.
		/// </summary>
		/// <returns></returns>
		protected override bool IsNameAttributeRendered()
		{
			return true;
		}
		#endregion
		#endregion
		#endregion
	}
}
